<form action="trangchu.php?key=upd" method="post" name="frm_upd_noi_dung" enctype="multipart/form-data">

<div style="display:block; float:left; padding-left:5px;"  class="content_right_group_admin box_shadow">
<?php 
	include_once("../../coms/xl_loaitrang.php");
	include_once("../../coms/xl_trang.php");
	include_once("../../coms/xl_tintuc.php");
	include_once("../../include/ckeditor/ckeditor.php");

	if(isset($_REQUEST['malt'])):		
		$MaLoaiTrang = $_REQUEST['malt'];
		
		$CKEditor_new = new CKEditor();
		$CKEditor_new->basePath = '../../include/ckeditor/';
		$CKEditor_new->config['height'] = 500;
		$CKEditor_new->config['width'] =680;
		
		$CKEditor_Title = new CKEditor();
		$CKEditor_Title->basePath = '../../include/ckeditor/';
		$CKEditor_Title->config['height'] = 80;
		$CKEditor_Title->config['width'] = 680;
		
		//Neu ma loai trang la dich vu
		if($MaLoaiTrang == 5):
			if(isset($_REQUEST['id'])&&isset($_REQUEST['madm'])):
				$madm = $_REQUEST['madm'];
				$id = $_REQUEST['id'];
				$dv = new xl_trang();
				$arr_dv = $dv->Load_DichVu_Theo_MaDichVu_MaTrang($madm, $id);			
				echo '<strong>- Tiêu đề:&nbsp;&nbsp;</strong>';
				echo '<input type="text" name="txt_TieuDe" value="'.$arr_dv['tentrang'].'" style="width:530px" class="border_radius"/>';
				echo '<br />';
				echo '<br />';
				echo '<strong>- Nội dung thu gọn :<br /></strong>';			
				$CKEditor_Title->editor("noidung_thugon",$arr_dv['noidungthugon']);
				echo '<strong>- Nội dung:<br /></strong>';			
				$CKEditor_new->editor("noidung_daydu",$arr_dv['noidung']);
				echo '<strong>- Hình ảnh:<br /></strong>';	
				if($arr_dv['hinhanh'] != "")
				{
					echo '<img src="../../'.$arr_dv['hinhanh'].'" >';
				}
				echo '<br />';
				echo '<strong>- Chọn hình mới:<br /></strong>';	
				echo '<input type="file" name="up_hinh" />';
				
				echo '<br />';
				echo '<strong>- Từ khóa:<br /></strong>';
				//Phuong - MOD - START - 06032012
				echo '<textarea name="tags" style="width:675px; height:100px" class="border_radius">'.$arr_dv['tags'].'</textarea>';		
				//Phuong - MOD - END - 06032012	
				
				//Toan - ADD - START - 08032012
				echo '<br /><br />';
				echo '<strong>- Mô tả:<br /></strong>';				
				echo '<textarea name="mota" style="width:675px; height:100px" class="border_radius">'.$arr_dv['mota'].'</textarea>';		
				echo '<input type="hidden" name="txt_imgURL" value="'.$arr_dv['hinhanh'].'" />';
				//Toan - ADD - END - 08032012	
			endif;
		elseif($MaLoaiTrang == 7 || $MaLoaiTrang == 6):
			if(isset($_REQUEST['id'])):
				$id = $_REQUEST['id'];
				$dv = new xl_tintuc();
				if($MaLoaiTrang == 7 )
				{
					$arr_dv = $dv->Load_TinTucTheoMa($id);				
				}
				else
				{
					$arr_dv = $dv->Load_KienThucTheoMa($id);	
				}				
				echo '<strong>- Tiêu đề:&nbsp;&nbsp;</strong><br \>';
				echo '<input type="text" name="txt_TieuDe" value="'.$arr_dv['tieude'].'" style="width:570px" class="border_radius"/>';
				echo '<br />';
				echo '<br />';
				echo '<strong>- Nội dung thu gọn :<br /></strong>';			
				$CKEditor_Title->editor("noidung_thugon",$arr_dv['noidungthugon']);
				echo '<br />';
				echo '<strong>- Nội dung đầy đủ :<br /></strong>';			
				$CKEditor_new->editor("noidung_daydu",$arr_dv['noidungdaydu']);
				echo '<strong>- Hình ảnh:<br /></strong>';	
				if($arr_dv['hinhanh'] != "")
				{
					echo '<img src="../../'.$arr_dv['hinhanh'].'" >';
				}
				echo '<br />';
				echo '<strong>- Chọn hình mới:<br /></strong>';	
				echo '<input type="file" name="up_hinh" />';
				echo '<br />';
				echo '<strong>- Từ khóa:<br /></strong>';
				//Phuong - MOD - START - 06032012
				echo '<textarea name="tags" style="width:675px; height:100px" class="border_radius">'.$arr_dv['tags'].'</textarea>';	
				//Phuong - MOD - END - 06032012		
				//Toan - ADD - START - 11032012
				echo '<br /><br />';
				echo '<strong>- Mô tả:<br /></strong>';				
				echo '<textarea name="mota" style="width:675px; height:100px" class="border_radius">'.$arr_dv['mota'].'</textarea>';
				echo '<input type="hidden" name="txt_imgURL" value="'.$arr_dv['hinhanh'].'" />';		
				//Toan - ADD - END - 11032012
			endif;
		//nguoc lai cap nhat loai trang	
		else:
			$lt = new xl_loaitrang();
			$arr_lt = $lt->LayLoaiTrangTheoMa($MaLoaiTrang);
			$CKEditor_new->editor("noidung_daydu",$arr_lt['noidung']);
			echo '<br />';
			echo '<strong>- Từ khóa:<br /></strong>';
			//Phuong - MOD - START - 06032012
			echo '<textarea name="tags" style="width:670px; height:100px" class="border_radius">'.$arr_lt['tags'].'</textarea>';	
			//Phuong - MOD - END - 06032012
			//Toan - ADD - START - 08032012
			echo '<br /><br />';
			echo '<strong>- Mô tả:<br /></strong>';				
			echo '<textarea name="mota" style="width:670px; height:100px" class="border_radius">'.$arr_lt['mota'].'</textarea>';		
			//Toan - ADD - END - 08032012	
		endif;		
		
?>	    



<div style="display:block; text-align:center; padding-top:10px;">
<br />
	<input type="hidden" name="hd_ma_loai_trang" value="<?php echo $MaLoaiTrang; ?>" />
    <?php 
		if ($MaLoaiTrang == 5):
			echo '<input type="hidden" name="hd_madm" value="'.$madm.'" />';
			echo '<input type="hidden" name="hd_id" value="'.$id.'" />';
		endif;
		if ($MaLoaiTrang == 7 || $MaLoaiTrang == 6):
			echo '<input type="hidden" name="hd_id" value="'.$id.'" />';
		endif; 
	?>
	 <!--Chức năng - Start-->

                        
                        <div style="padding:10px;">

                        	 <input type="submit" name="btn_CapNhat" value="Hoàn tất" class="button medium blue">
                             <a href="trangchu.php?key=alb" class="button medium blue" target="_blank">Thư viện hình ảnh</a>
                        </div>
 
                   
		<!--Chức năng - Start-->
	
</div>
</div>
</form>

<?php
endif;
if(isset($_POST['btn_CapNhat'])):
	//Neu ma loai trang la dich vu
	$maloai = $_POST['hd_ma_loai_trang'];
	if($maloai == 5 && is_numeric($_POST['hd_id'])&& is_numeric($_POST['hd_madm'])):
		$TieuDe = $_POST['txt_TieuDe'];
		$id = $_POST['hd_id'];		
		$maDanhMuc = $_POST['hd_madm'];	
		$NoiDungThuGon = $_POST['noidung_thugon'];
		$NoiDung = $_POST['noidung_daydu'];
		$Tags = $_POST['tags'];	
		//Toan - ADD - START - 08032012
		$motaTags = $_POST['mota'];		
		//Toan - ADD - END - 08032012	
		
		//Toan - ADD - START - 10062012
		//update hinh
		$imgURL = $_POST['txt_imgURL'];		
		$kq=array(0);
		if($_FILES['up_hinh']['name'] != NULL)
		{
			//xoa hinh anh cu
			if($imgURL != "")
			{
				unlink("../../".$imgURL."");	
			}
			$kq=up_hinh_dich_vu();
			if($kq[2]==true)
			{
				$tenhinhanh = "images/dichvu/".$kq[1];
				$capnhat_hinhanh = new xl_trang();
				$ketqua = $capnhat_hinhanh->capnhat_hinhanh($id, $tenhinhanh);		
			}
		}
		
		//Toan - ADD - END - 10062012
		$tendichvu = trim(strtolower($TieuDe));
		include_once("../../include/convert_link.php");
		$conv = new convert_link();
		$tendichvu_convert = $conv->convert_vi_to_en($tendichvu);
		$kt = new xl_trang();
		//kiem tra neu k thay doi ten tat
		$kt_tontai = $kt->kt_ten_viet_tat_theo_id($id, $tendichvu_convert);
		
		
		if($kt_tontai): // neu co thay doi thi cap nhat
						
			$capnhat = new xl_trang();
			//Toan - MOD - them mo ta - START - 08032012
			$ketqua = $capnhat->capnhat($id, $maDanhMuc, $TieuDe,$NoiDungThuGon, $NoiDung, $Tags, $_SESSION['MaNguoiDung'], $tendichvu_convert, $motaTags);
			//Toan - MOD - them mo ta - END - 08032012
			//Phuong - ADD - START - 06032012
			echo '<script type="text/javascript">
				alert("Cập nhật thành công!!!");				
			</script>';
			//Phuong - ADD - END - 06032012
			//Phuong - MOD - START - 06032012
			echo '<meta http-equiv="refresh" content="0;url=trangchu.php?key=nd&malt='.$maloai.'&madm='.$maDanhMuc.'&id='.$id.'"  />';
			//Phuong - MOD - END - 06032012
		else: //neu co thay doi ten tat. phai kt ten tat co bi trung voi nhung ten khac k?
			$kt2 = new xl_trang();
			$kt_tontai2 = $kt2->kt_ten_viet_tat_trung_theo_id($id, $tendichvu_convert);
			if($kt_tontai2): //neu bi trung ten
				echo '<script type="text/javascript">
					alert("TĂªn dá»‹ch vá»¥ Ä‘Ă£ tá»“n táº¡i. Vui lĂ²ng nháº­p láº¡i!!!");	
					window.location="trangchu.php?key=upd&malt=5&madm='.$maDanhMuc.'&id='.$id.'";				
				</script>';
		
			else: // k trung ten thĂ¬ update
				$capnhat = new xl_trang();
				$ketqua = $capnhat->capnhat($id, $maDanhMuc, $TieuDe,$NoiDungThuGon, $NoiDung, $Tags, $_SESSION['MaNguoiDung'], $tendichvu_convert, $motaTags);
				//Phuong - ADD - START - 06032012
				echo '<script type="text/javascript">
					alert("Cập nhật thành công!!!");				
				</script>';
				//Phuong - ADD - END - 06032012
				echo '<meta http-equiv="refresh" content="0;url=trangchu.php?key=nd&malt='.$maloai.'&madm='.$maDanhMuc.'&id='.$id.'"  />';
			endif;

		endif;
	elseif($maloai == 7 && is_numeric($_POST['hd_id'])):
		$TieuDe = $_POST['txt_TieuDe'];
		$id = $_POST['hd_id'];
		$NoiDungThuGon = $_POST['noidung_thugon'];
		$NoiDungDayDu = $_POST['noidung_daydu'];		
		$Tags = $_POST['tags'];	
		//Toan - ADD - START - 11032012
		$motaTags = $_POST['mota'];		
		$imgURL = $_POST['txt_imgURL'];
		//Toan - ADD - END - 11032012
		$kq=array(0);
		if($_FILES['up_hinh']['name'] != NULL)
		{
			//xoa hinh anh cu
			if($imgURL != "")
			{
				unlink("../../".$imgURL."");	
			}
			$kq=up_hinh();
			$now = getdate();
			$currentDate = $now["year"] . "-" . $now["mon"] . "-" . $now["mday"]; 
			$ngaydang = $currentDate;
			if($kq[2]==true)
			{
				$ten = "images/tintuc/".$kq[1];
				$capnhat = new xl_tintuc();
				$ketqua = $capnhat->capnhat($id, $TieuDe, $NoiDungThuGon,$NoiDungDayDu, $Tags, $ten,$_SESSION['MaNguoiDung'],$motaTags);
				//Phuong - ADD - START - 06032012
				echo '<script type="text/javascript">
					alert("Cập nhật thành công!!!");				
				</script>';
				//Phuong - ADD - END - 06032012
				echo '<meta http-equiv="refresh" content="0;url=trangchu.php?key=nd&malt='.$maloai.'&id='.$id.'"  />';
			}
		}
		else
		{	
			$capnhat1 = new xl_tintuc();
			$ketqua = $capnhat1->capnhat1($id, $TieuDe, $NoiDungThuGon,$NoiDungDayDu, $Tags, $_SESSION['MaNguoiDung'],$motaTags);
			//Phuong - ADD - START - 06032012
			echo '<script type="text/javascript">
				alert("Cập nhật thành công!!!");				
			</script>';
			//Phuong - ADD - END - 06032012
			echo '<meta http-equiv="refresh" content="0;url=trangchu.php?key=nd&malt='.$maloai.'&id='.$id.'"  />';
		}
	elseif($maloai == 6 && is_numeric($_POST['hd_id'])):
		$TieuDe = $_POST['txt_TieuDe'];
		$id = $_POST['hd_id'];
		$NoiDungThuGon = $_POST['noidung_thugon'];
		$NoiDungDayDu = $_POST['noidung_daydu'];
		$Tags = $_POST['tags'];	
		//Toan - ADD - START - 11032012
		$motaTags = $_POST['mota'];		
		$imgURL = $_POST['txt_imgURL'];
		//Toan - ADD - END - 11032012
		$kq=array(0);
		if($_FILES['up_hinh']['name'] != NULL)
		{
			//xoa hinh anh cu
			if($imgURL != "")
			{
				unlink("../../".$imgURL."");	
			}
			$kq=up_hinh();
			$now = getdate();
			$currentDate = $now["year"] . "-" . $now["mon"] . "-" . $now["mday"]; 
			$ngaydang = $currentDate;
			if($kq[2]==true)
			{
				$ten = "images/tintuc/".$kq[1];
				$capnhat = new xl_tintuc();
				$ketqua = $capnhat->capnhat($id, $TieuDe, $NoiDungThuGon,$NoiDungDayDu, $Tags, $ten,$_SESSION['MaNguoiDung'],$motaTags);
				//Phuong - ADD - START - 06032012
				echo '<script type="text/javascript">
					alert("Cập nhật thành công!!!");				
				</script>';
				//Phuong - ADD - END - 06032012
				echo '<meta http-equiv="refresh" content="0;url=trangchu.php?key=nd&malt='.$maloai.'&id='.$id.'"  />';
			}
		}
		else
		{	
			$capnhat1 = new xl_tintuc();
			$ketqua = $capnhat1->capnhat1($id, $TieuDe, $NoiDungThuGon,$NoiDungDayDu, $Tags,$_SESSION['MaNguoiDung'],$motaTags);
			//Phuong - ADD - START - 06032012
			echo '<script type="text/javascript">
				alert("Cập nhật thành công!!!");				
			</script>';
			//Phuong - ADD - END - 06032012
			echo '<meta http-equiv="refresh" content="0;url=trangchu.php?key=nd&malt='.$maloai.'&id='.$id.'"  />';
		}
	else:
		$NoiDung = $_POST['noidung_daydu'];
		$Tags = $_POST['tags'];
		$mota = $_POST['mota'];		
		$capnhat2 = new xl_loaitrang();
		$ketqua2 = $capnhat2->capnhat($maloai,$NoiDung,$Tags,$mota);
		echo '<script type="text/javascript">
				alert("Cập nhật thành công!!!");				
			</script>';
		echo '<meta http-equiv="refresh" content="0;url=trangchu.php?key=nd&malt='.$maloai.'"  />';
	endif;
endif;

function up_hinh()
{
	$result = "";
	define('UPLOAD_DIR','../../images/tintuc/');
	define('MAX_FILES_SIZE',10000000);
	$file = str_replace(" ","_",$_FILES['up_hinh']['name']);
	$max = number_format(MAX_FILES_SIZE/1024,1)." KB";
	$premitted = array('image/gif','image/jpeg','image/pjpeg','image/png');
	$sizeOK = false;
	$typeOK = false;
	$success = false;
	$kq=array(3);
	if($_FILES['up_hinh']['size']>0 && $_FILES['up_hinh']['size']<= MAX_FILES_SIZE)
	{
		$sizeOK = true;
	}
	foreach($premitted as $type)
	{
		if($type == $_FILES['up_hinh']['type'])
		{
			$typeOK = true;
			break;
		}
	}
	
	if($sizeOK && $typeOK)
	{
		switch($_FILES['up_hinh']['error'])
		{
			case 0:
				if(!file_exists(UPLOAD_DIR.$file))
				{		
					$success = move_uploaded_file($_FILES['up_hinh']['tmp_name'],UPLOAD_DIR.$file);
					$kq[1]=$file;
					$kq[0] = UPLOAD_DIR;
					
				}
				else
				{
					$now  = date("YmdHis",time());
					$file = $now.$file;
					$success = move_uploaded_file($_FILES['up_hinh']['tmp_name'],UPLOAD_DIR.$file);
					$kq[1]=$file;
					$kq[0] = UPLOAD_DIR;
				}
				if($success){
					$result = "$file upload thĂ nh cĂ´ng";
				}
				else{
					$result = "Lỗi upload $file. Vui lòng chọn lại.";
				}
				break;
			case 3:
				$result = "Lá»—i upload $file.Vui long thá»­ láº¡i.";
			default:
				$result = "Lá»—i há»‡ thá»‘ng upload $file.Vui long thá»­ láº¡i.";
		}
	}
	elseif($_FILES['up_hinh']['error']==4){
		$result = "ChÆ°a chá»n file upload";
	}
	else{
		$result = "Lá»—i file upload. Maximum size: $max. File upload pháº£i lĂ  gif, jpg, png.";
	}
/*	if($success == TRUE)
	{
		echo '<script language="javascript">
				window.alert("'.$result.'");
			</script>';
	}
	else
	{
		echo '<script language="javascript">
				window.alert("'.$result.'");
			</script>';
	}*/
	$kq[2] = $success;
	return $kq;
}

function up_hinh_dich_vu()
{
	$result = "";
	define('UPLOAD_DIR','../../images/dichvu/');
	define('MAX_FILES_SIZE',10000000);
	$file = str_replace(" ","_",$_FILES['up_hinh']['name']);
	$max = number_format(MAX_FILES_SIZE/1024,1)." KB";
	$premitted = array('image/gif','image/jpeg','image/pjpeg','image/png');
	$sizeOK = false;
	$typeOK = false;
	$success = false;
	$kq=array(3);
	if($_FILES['up_hinh']['size']>0 && $_FILES['up_hinh']['size']<= MAX_FILES_SIZE)
	{
		$sizeOK = true;
	}
	foreach($premitted as $type)
	{
		if($type == $_FILES['up_hinh']['type'])
		{
			$typeOK = true;
			break;
		}
	}
	
	if($sizeOK && $typeOK)
	{
		switch($_FILES['up_hinh']['error'])
		{
			case 0:
				if(!file_exists(UPLOAD_DIR.$file))
				{		
					$success = move_uploaded_file($_FILES['up_hinh']['tmp_name'],UPLOAD_DIR.$file);
					$kq[1]=$file;
					$kq[0] = UPLOAD_DIR;
					
				}
				else
				{
					$now  = date("YmdHis",time());
					$file = $now.$file;
					$success = move_uploaded_file($_FILES['up_hinh']['tmp_name'],UPLOAD_DIR.$file);
					$kq[1]=$file;
					$kq[0] = UPLOAD_DIR;
				}
				if($success){
					$result = "$file upload thĂ nh cĂ´ng";
				}
				else{
					$result = "Lỗi upload $file. Vui lòng chọn lại.";
				}
				break;
			case 3:
				$result = "Lá»—i upload $file.Vui long thá»­ láº¡i.";
			default:
				$result = "Lá»—i há»‡ thá»'ng upload $file.Vui long thá»­ láº¡i.";
		}
	}
	elseif($_FILES['up_hinh']['error']==4){
		$result = "ChÆ°a chá»n file upload";
	}
	else{
		$result = "Lá»—i file upload. Maximum size: $max. File upload pháº£i lĂ  gif, jpg, png.";
	}
/*	if($success == TRUE)
	{
		echo '<script language="javascript">
				window.alert("'.$result.'");
			</script>';
	}
	else
	{
		echo '<script language="javascript">
				window.alert("'.$result.'");
			</script>';
	}*/
	$kq[2] = $success;
	return $kq;
}
?>
